import { loginAPI, getUserInfoApi, PersonalDetailsAPI } from '@/api/user'
import { setToken as setTokenPro, getToken, removeToken } from '@/utils/auth'

export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    // 定义存储对象
    setUser: {}

  },
  mutations: {
    satToken(state, nowToken) {
      state.token = nowToken
      setTokenPro(nowToken)
    },
    amendUset(state, row) {
      state.setUser = row
    },
    logout(state) {
      // 清空token
      state.token = ''
      // 清空个人信息
      state.setUser = {}
      // 删除Cook里的token
      removeToken()
    }
  },
  actions: {
    async login(store, data) {
      // return new Promise((resolve, reject) => {
      //   // 发送登录请求
      //   loginAPI(data).then((res) => {
      //     // 成功时更新数据
      //     store.commit('satToken', res.data)
      //     resolve(res)
      //   }).catch(error => {
      //     // 失败时返回值
      //     reject(error)
      //   })
      // })
      const res = await loginAPI(data)
      store.commit('satToken', res.data)
      return res
    },
    async peraonLsData(store) {
      const res = await getUserInfoApi()
      console.log(res)
      const res2 = await PersonalDetailsAPI(res.data.userId)
      const compilations = {
        ...res.data,
        ...res2.data
      }
      store.commit('amendUset', compilations)
      return compilations
    }
  },
  getters: {}
}
